package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysRoleMapper;
import com.sofwin.mapper.SysRoleResourceMapper;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleResource;
import com.sofwin.pojo.SysRoleResourceExample;
import com.sofwin.service.RoleService;
import com.sofwin.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2023/1/3 9:38
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;
    @Autowired
    private SysRoleResourceMapper roleResourceMapper;
    @Override
    public PageInfo<SysRole> queryByPage(SysRole role, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(role.getRoleName())){
            criteria.andRoleNameLike("%"+role.getRoleName()+"%");
        }
        List<SysRole> roles = mapper.selectByExampleWithBLOBs(example);
        return new PageInfo<>(roles);
    }

    @Override
    public SysRole queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveOrUpdate(SysRole role,Integer[] resourceIds) {
        if(role.getId()!=null){
            // 修改更新时间
            role.setGmtModified(DateUtil.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
            int flag = 0;
            // 第一步修改角色信息
            flag+= mapper.updateByPrimaryKeyWithBLOBs(role);

            // 第二部修改权限信息 sys_role_resouce
            // 一般是根据id来修改 --
            // 根据条件更新  role_id resource_id
            // sys_role_resource表修改时
            // role_id=9的全部删除
            // 重新插入
            SysRoleResourceExample example = new SysRoleResourceExample();
            SysRoleResourceExample.Criteria criteria = example.createCriteria();
            criteria.andRoleIdEqualTo(role.getId());
            roleResourceMapper.deleteByExample(example);
            for(Integer resourceId:resourceIds){
                // 构建SysRoleResource对象
                SysRoleResource roleResource = new SysRoleResource();
                roleResource.setRoleId(role.getId());
                roleResource.setResourceId(resourceId);
                flag+= roleResourceMapper.insertSelective(roleResource);
            }
            return flag==(resourceIds.length+1)?true:false;
        }
        role.setGmtCreate(DateUtil.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        // 先保存角色信息
        int flag =0;
        // insert获取自增主键(2种   selectKey useGeneraterKey)
        flag+= mapper.insertSelective(role);
        // role.getId()
        Integer roleId = role.getId();
        // 保存权限信息(sys_role_resource)
        // id(自增） role_id resource_id(循环resourceids)
        for(Integer resourceId:resourceIds){
            // 构建SysRoleResource对象
            SysRoleResource roleResource = new SysRoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            flag+= roleResourceMapper.insertSelective(roleResource);
        }
         return flag==(resourceIds.length+1)?true:false;
    }

    @Override
    public boolean removeById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeByIds(Integer[] ids) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        return mapper.deleteByExample(example)==ids.length?true:false;
    }

    @Override
    public boolean queryByName(String roleName) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleNameEqualTo(roleName);
        List<SysRole> roles = mapper.selectByExample(example);
        if(roles!=null && roles.size()!=0){
            return false;
        }
        return true;
    }
}
